{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. 匿名函数 lambda"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.1 匿名函数基础\n",
    "\n",
    "```\n",
    "lambda argument1, argument2,... argumentN : expression\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "square = lambda x: x**2\n",
    "\n",
    "print(square(2))\n",
    "def square1(x):\n",
    "    return x**2\n",
    "\n",
    "print(square1(2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- lambda 是一个表达式（expression），并不是一个语句（statement）\n",
    "- lambda 的主体是只有一行的简单表达式，并不能扩展成一个多行的代码块\n",
    "- "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "squared = map(lambda x: x**2, [1, 2, 3, 4, 5])\n",
    "\n",
    "def square(x):\n",
    "    return x**2\n",
    "\n",
    "squared = map(square, [1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.2 函数式编程\n",
    "\n",
    "- map\n",
    "- filter\n",
    "- reduce "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = [1, 2, 3, 4, 5]\n",
    "new_list = map(lambda x: x * 2, l) # [2， 4， 6， 8， 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (2824650344.py, line 2)",
     "output_type": "error",
     "traceback": [
      "  \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[6]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[31m    \u001b[39m\u001b[31mpython -m timeit -s'xs=range(1000000)' 'map(lambda x: x*2, xs)'\u001b[39m\n              ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import timeit\n",
    "python -m timeit -s'xs=range(1000000)' 'map(lambda x: x*2, xs)'\n",
    "\n",
    "\n",
    "python -m timeit -s'xs=range(1000000)' '[x * 2 for x in xs]'\n",
    "\n",
    "\n",
    "python -m timeit -s'xs=range(1000000)' 'l = []' 'for i in xs: l.append(i * 2)'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ben', 'mike', 'lucy']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'mike': 10, 'lucy': 2, 'ben': 30}\n",
    "\n",
    "sorted(d, key=lambda x: x[1])\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
